<?php

/**
 * This is the model class for table "tenant".
 *
 * The followings are the available columns in table 'tenant':
 * @property integer $id
 * @property string $MiddleInitial
 * @property string $DateOfBirth
 * @property string $LastFour
 * @property string $OtherLastName
 * @property string $OtherLastName2
 * @property string $LasFreeAcess
 * @property integer $iduser
 *
 * The followings are the available model relations:
 * @property Leaseviolations[] $leaseviolations
 * @property Occupancy[] $occupancys
 * @property User $iduser0
 * @property Tenantpaymenthistory[] $tenantpaymenthistories
 * @property Tenantrebuttal[] $tenantrebuttals
 * @property Tenantreportedcurrentaddress[] $tenantreportedcurrentaddresses
 */
class Tenant extends CActiveRecord
{
	public $max;
	public $FirstName, $LastName, $userName, $email;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Tenant the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tenant';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		//	array('iduser', 'authenticate'),
			array('iduser', 'numerical', 'integerOnly'=>true),
			array('MiddleInitial, OtherLastName, OtherLastName2', 'length', 'max'=>15),
			array('LastFour', 'length', 'max'=>4),
			
			array('LasFreeAcess', 'length', 'max'=>45),
			array('DateOfBirth', 'safe'),
			array('MiddleInitial','required'),
			array('FirstName,LastName,email,userName', 'safe', 'on'=>'search'),
			
			
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, MiddleInitial, DateOfBirth, LastFour, OtherLastName, OtherLastName2, LasFreeAcess, iduser', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'leaseviolations' => array(self::HAS_MANY, 'Leaseviolations', 'idTenant'),
			'occupancys' => array(self::HAS_MANY, 'Occupancy', 'idTenant'),
			'iduser0' => array(self::BELONGS_TO, 'User', 'iduser'),
			'tenantpaymenthistories' => array(self::HAS_MANY, 'Tenantpaymenthistory', 'idTenant'),
			'tenantrebuttals' => array(self::HAS_MANY, 'Tenantrebuttal', 'idTenant'),
			'tenantreportedcurrentaddresses' => array(self::HAS_ONE, 'Tenantreportedcurrentaddress', 'idTenant'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'MiddleInitial' => 'Middle Initial',
			'DateOfBirth' => 'Date Of Birth',
			'LastFour' => 'Last Four',
			'OtherLastName' => 'Other Last Name',
			'OtherLastName2' => 'Other Last Name2',
			'LasFreeAcess' => 'Last Free Acess',
			'iduser' => 'Iduser',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		 $sort = new CSort();
		 $sort->attributes = array(

		   'FirstName'=>array(
		      'asc'=>$this->FirstName,
		      'desc'=>$this->FirstName,
				 ),
		
			'MiddleInitial' => array(
               'asc'=>'MiddleInitial',
               'desc'=>'MiddleInitial desc',
             ),
				 
		   'LastName'=>array(
		      'asc'=>$this->LastName,
		      'desc'=>$this->LastName,
				 ),
				 
			'DateOfBirth' => array(
               'asc'=>'DateOfBirth',
               'desc'=>'DateOfBirth desc',
             ),		 
		
		   'userName'=>array(
		      'asc'=>$this->userName,
		      'desc'=>$this->userName,
		 		),
		 		
		 	'email'=>array(
		      'asc'=>$this->email,
		      'desc'=>$this->email,
				 ),	
		 
		 );

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('MiddleInitial',$this->MiddleInitial,true);
		$criteria->compare('DateOfBirth',$this->DateOfBirth,true);
		$criteria->compare('LastFour',$this->LastFour,true);
		$criteria->compare('OtherLastName',$this->OtherLastName,true);
		$criteria->compare('OtherLastName2',$this->OtherLastName2,true);
		
		$criteria->compare('LasFreeAcess',$this->LasFreeAcess,true);
		$criteria->compare('FirstName', $this->FirstName, true);
		$criteria->compare('LastName', $this->LastName, true);
		$criteria->compare('userName', $this->userName, true);
		$criteria->compare('email', $this->email, true);
		$criteria->with=array('iduser0');
		
		/*$criteria->compare('iduser0.FirstName',$this->iduser0->FirstName,true);
		$criteria->with[]='iduser0';
		$criteria->addSearchCondition("iduser0.id",$this->iduser);*/

		/*//$criteria->compare('iduser',$this->iduser);
		
		$criteria->compare('LastName',$this->iduser0->LastName,true);
		$criteria->compare('userName',$this->iduser0->userName,true);
		//$criteria->compare('Password',$this->Password,true);
		$criteria->compare('email',$this->iduser0->email,true);
		$criteria->compare('Status',$this->iduser0->Status,true);
		$criteria->with('iduser0');
		*/

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria ,
			'sort'=>$sort,
			/*.'with'=>array('iduser0'),
			
			'together'=>true,
			'condition'=>'idTenant='.$this->id,*/
		));
	}
	
	
	
	public function setTenantUserName()
	{
		/*$criteria=new CDbCriteria;
		$criteria->select='max(id) ';  // only select the 'title' column
		$tenant=Tenant::model()->find($criteria);
		//$tname=Helper::fomatNumber((int)$tenant->id + 1);
		echo $tenant->max;*/
		
		$max = Yii::app()->db->createCommand("SELECT max(`id`) as `mnum` FROM `tenant` WHERE 1")->queryScalar();

		return 'RS'.Helper::fomatNumber($max+1);
		
	
	}
	
	 public function afterDelete()
	  {
		parent::afterDelete();
		User::model()->deleteByPk($this->iduser); //field value you want to access
	  }
		
	
}